﻿using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace CommLib.Security
{
    public class Sha2Encrypt
    {
        /// <summary>
        /// Sha256加密
        /// </summary>
        /// <param name="encryptString">要加密的字符串</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <exception cref="System.FormatException"></exception>
        /// <exception cref="System.ObjectDisposedException"></exception>
        /// <exception cref="System.Reflection.TargetInvocationException"></exception>
        /// <exception cref="System.Text.EncoderFallbackException"></exception>
        /// <returns></returns>
        public static string Sha256(string encryptString)
        {
            if (string.IsNullOrEmpty(encryptString))
                return null;
            using (var sha256 = SHA256.Create())
            {
                if (sha256 == null)
                    return null;
                var source = sha256.ComputeHash(Encoding.UTF8.GetBytes(encryptString));
                return ByteToString(source);
            }
        }
        
        /// <summary>
        /// Sha384加密
        /// </summary>
        /// <param name="encryptString">要加密的字符串</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <exception cref="System.FormatException"></exception>
        /// <exception cref="System.ObjectDisposedException"></exception>
        /// <exception cref="System.Reflection.TargetInvocationException"></exception>
        /// <exception cref="System.Text.EncoderFallbackException"></exception>
        /// <returns></returns>
        public static string Sha384(string encryptString)
        {
            if (string.IsNullOrEmpty(encryptString))
                return null;
            using (var sha384 = SHA384.Create())
            {
                if (sha384 == null)
                    return null;
                var source = sha384.ComputeHash(Encoding.UTF8.GetBytes(encryptString));
                return ByteToString(source);
            }
        }
        
        /// <summary>
        /// Sha512加密
        /// </summary>
        /// <param name="encryptString">要加密的字符串</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <exception cref="System.FormatException"></exception>
        /// <exception cref="System.ObjectDisposedException"></exception>
        /// <exception cref="System.Reflection.TargetInvocationException"></exception>
        /// <exception cref="System.Text.EncoderFallbackException"></exception>
        /// <returns></returns>
        public static string Sha512(string encryptString)
        {
            if (string.IsNullOrEmpty(encryptString))
                return null;
            using (var sha512 = SHA512.Create())
            {
                if (sha512 == null)
                    return null;
                var source = sha512.ComputeHash(Encoding.UTF8.GetBytes(encryptString));
                return ByteToString(source);
            }
        }
        private static string ByteToString(IEnumerable<byte> source)
        {
            var sBuilder = new StringBuilder();
            foreach (var s in source)
            {
                sBuilder.Append(s.ToString("x2"));
            }
            return sBuilder.ToString();
        }
    }
}
